package cn.lxh.org.service.impl;

import cn.lxh.basic.service.impl.BaseServieceImpl;
import cn.lxh.basic.utils.MD5Utils;
import cn.lxh.basic.utils.PageList;
import cn.lxh.basic.utils.StrUtils;
import cn.lxh.org.domain.Department;
import cn.lxh.org.domain.Employee;
import cn.lxh.org.mapper.EmployeeMapper;

import cn.lxh.org.service.IEmployeeService;
import cn.lxh.user.domain.Logininfo;
import cn.lxh.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServieceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void save(Employee employee) {
        initEmployee(employee);
        //要级联保存logininfo
        Logininfo logininfo = emp2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }
    private Logininfo emp2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //这个工具类提供了一个复制相同字段的方式
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//注意：一定要手动设置，这是员工
        return  logininfo;
    }
    /**
     * 初始化员工表
     * @param employee
     */
    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
    }




}
